package com.situ.company.department.dao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class DepartmentDao implements IDepartmentDao {

    public Integer insert(DepartmentModel model) {
        String sql = "insert into department (code,name,tel) values (?,?,?)";
        // List<Object> vals = Arrays.asList(model.getCode(), model.getName(), model.getTel());
        return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getTel());
    }
    
    public List<Integer> insertList(List<DepartmentModel> list) {
        List<Integer> res = new ArrayList<>();
        for(DepartmentModel model : list)
            res.add(insert(model));
        return res;
    }
    
    public Integer deleteByCode(DepartmentModel model) {
        String sql = "delete from department where code = ?";
        return JDBCUtil.update(sql.toString(), model.getCode());
    }

    public Integer delete(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("delete from department");
        List<Object> vals = appendWhere(sql, model);
        return JDBCUtil.update(sql.toString(), vals);
    }

    public Integer updateAll(DepartmentModel model) {
        String sql = "update department set name=?,tel=? where code=?";
        return JDBCUtil.update(sql, model.getName(), model.getTel(), model.getCode());
    }
    
    public Integer updateActive(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("update department set id=id ");
        List<Object> vals = new ArrayList<>();
        String name = model.getName();
        if(name != null) {
            sql.append(" ,name = ? ");
            vals.add(name);
        }
        String tel = model.getTel();
        if(tel != null) {
            sql.append(" ,tel = ? ");
            vals.add(tel);
        }
        sql.append(" where code = ? ");
        vals.add(model.getCode());
        return JDBCUtil.update(sql.toString(), vals);
    }
    
    public List<DepartmentModel> selectList(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select code,name,tel,(select count(1) from employee where code_dept=department.code) count from department ");
        List<Object> vals = appendWhere(sql, model);
//        Map<String, String> fields = new HashMap<>();
//        fields.put("cod", "code");
//        fields.put("name", "name");
//        fields.put("tel", "tel");
        List<String> props = Arrays.asList("code","name","tel","count");
        return JDBCUtil.queryList(sql.toString(), vals, DepartmentModel.class,  props);
    }
    
    public DepartmentModel selectModel(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select code,name,tel from department ");
        List<Object> vals = appendWhere(sql, model);
        List<String> props = Arrays.asList("code","name","tel");
        return JDBCUtil.queryModel(sql.toString(), vals, DepartmentModel.class,  props);
    }
    
    private List<Object> appendWhere(StringBuffer sql, DepartmentModel model){
        List<Object> vals = new ArrayList<>();
        sql.append(" where 1=1 ");
        String code = model.getCode();
        if(!FmtEmpty.isEmpty(code)) {
            sql.append(" and code like ? ");
            vals.add(code);
        }
        String name = model.getName();
        if(!FmtEmpty.isEmpty(name)) {
            sql.append(" and name like ? ");
            vals.add(name);
        }
        String tel = model.getTel();
        if(!FmtEmpty.isEmpty(tel)) {
            sql.append(" and tel like ? ");
            vals.add(tel);
        }
        if(model.isPageOn()) {
            sql.append(" limit ?,? ");
            vals.add(model.getRowStart());
            vals.add(model.getPageLimit());
        }
        return vals;
    }

    public Integer selectCount(DepartmentModel m0) {
        StringBuffer sql = new StringBuffer("select count(1) from department ");
        List<Object> vals = appendWhere(sql, m0);
        return JDBCUtil.queryInt(sql.toString(), vals);
    }

}
